#Boa:Frame:FPrincipal2

import wx
import psycopg2
import psycopg2.extras
import sys
import pprint
from FAsistencia import FAsistencia

def create(parent):
    return FPrincipal2(parent)

TempCodigo2=''

[wxID_FPRINCIPAL2, wxID_FPRINCIPAL2B0, wxID_FPRINCIPAL2B1, wxID_FPRINCIPAL2B2, 
 wxID_FPRINCIPAL2B3, wxID_FPRINCIPAL2B4, wxID_FPRINCIPAL2B5, 
 wxID_FPRINCIPAL2B6, wxID_FPRINCIPAL2B7, wxID_FPRINCIPAL2B8, 
 wxID_FPRINCIPAL2B9, wxID_FPRINCIPAL2BCLEAR, wxID_FPRINCIPAL2BUTTON4, 
 wxID_FPRINCIPAL2BUTTON5, wxID_FPRINCIPAL2BUTTON6, wxID_FPRINCIPAL2ENTRAR, 
 wxID_FPRINCIPAL2IDN, wxID_FPRINCIPAL2N, wxID_FPRINCIPAL2PANEL1, 
 wxID_FPRINCIPAL2PANEL2, wxID_FPRINCIPAL2SALIR, wxID_FPRINCIPAL2TEXTCODIGO, 
] = [wx.NewId() for _init_ctrls in range(22)]

class FPrincipal2(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FPRINCIPAL2, name=u'FPrincipal2',
              parent=prnt, pos=wx.Point(506, 167), size=wx.Size(300, 220),
              style=wx.DEFAULT_FRAME_STYLE, title=u'Ingresar')
        self.SetClientSize(wx.Size(300, 220))

        self.panel1 = wx.Panel(id=wxID_FPRINCIPAL2PANEL1, name='panel1',
              parent=self, pos=wx.Point(0, 0), size=wx.Size(300, 220),
              style=wx.TAB_TRAVERSAL)

        self.B1 = wx.Button(id=wxID_FPRINCIPAL2B1, label=u'1', name=u'B1',
              parent=self.panel1, pos=wx.Point(24, 60), size=wx.Size(50, 50),
              style=0)
        self.B1.Bind(wx.EVT_BUTTON, self.OnB1Button, id=wxID_FPRINCIPAL2B1)

        self.B4 = wx.Button(id=wxID_FPRINCIPAL2B4, label=u'4', name=u'B4',
              parent=self.panel1, pos=wx.Point(24, 110), size=wx.Size(50, 50),
              style=0)
        self.B4.Bind(wx.EVT_BUTTON, self.OnB4Button, id=wxID_FPRINCIPAL2B4)

        self.B7 = wx.Button(id=wxID_FPRINCIPAL2B7, label=u'7', name=u'B7',
              parent=self.panel1, pos=wx.Point(24, 160), size=wx.Size(50, 50),
              style=0)
        self.B7.Bind(wx.EVT_BUTTON, self.OnB7Button, id=wxID_FPRINCIPAL2B7)

        self.panel2 = wx.Panel(id=wxID_FPRINCIPAL2PANEL2, name='panel2',
              parent=self.panel1, pos=wx.Point(-48, 224), size=wx.Size(350,
              148), style=wx.TAB_TRAVERSAL)

        self.button4 = wx.Button(id=wxID_FPRINCIPAL2BUTTON4, label='button1',
              name='button4', parent=self.panel2, pos=wx.Point(24, 160),
              size=wx.Size(50, 50), style=0)

        self.button6 = wx.Button(id=wxID_FPRINCIPAL2BUTTON6, label='button1',
              name='button6', parent=self.panel2, pos=wx.Point(24, 110),
              size=wx.Size(50, 50), style=0)
        self.button6.Bind(wx.EVT_BUTTON, self.OnButton2Button,
              id=wxID_FPRINCIPAL2BUTTON6)

        self.button5 = wx.Button(id=wxID_FPRINCIPAL2BUTTON5, label='button1',
              name='button5', parent=self.panel2, pos=wx.Point(24, 60),
              size=wx.Size(50, 50), style=0)

        self.B2 = wx.Button(id=wxID_FPRINCIPAL2B2, label=u'2', name=u'B2',
              parent=self.panel1, pos=wx.Point(75, 60), size=wx.Size(50, 50),
              style=0)
        self.B2.Bind(wx.EVT_BUTTON, self.OnB2Button, id=wxID_FPRINCIPAL2B2)

        self.B3 = wx.Button(id=wxID_FPRINCIPAL2B3, label=u'3', name=u'B3',
              parent=self.panel1, pos=wx.Point(126, 60), size=wx.Size(50, 50),
              style=0)
        self.B3.Bind(wx.EVT_BUTTON, self.OnB3Button, id=wxID_FPRINCIPAL2B3)

        self.B5 = wx.Button(id=wxID_FPRINCIPAL2B5, label=u'5', name=u'B5',
              parent=self.panel1, pos=wx.Point(75, 110), size=wx.Size(50, 50),
              style=0)
        self.B5.Bind(wx.EVT_BUTTON, self.OnB5Button, id=wxID_FPRINCIPAL2B5)

        self.B8 = wx.Button(id=wxID_FPRINCIPAL2B8, label=u'8', name=u'B8',
              parent=self.panel1, pos=wx.Point(75, 160), size=wx.Size(50, 50),
              style=0)
        self.B8.Bind(wx.EVT_BUTTON, self.OnB8Button, id=wxID_FPRINCIPAL2B8)

        self.B6 = wx.Button(id=wxID_FPRINCIPAL2B6, label=u'6', name=u'B6',
              parent=self.panel1, pos=wx.Point(126, 110), size=wx.Size(50, 50),
              style=0)
        self.B6.Bind(wx.EVT_BUTTON, self.OnB6Button, id=wxID_FPRINCIPAL2B6)

        self.B9 = wx.Button(id=wxID_FPRINCIPAL2B9, label=u'9', name=u'B9',
              parent=self.panel1, pos=wx.Point(126, 160), size=wx.Size(50, 50),
              style=0)
        self.B9.Bind(wx.EVT_BUTTON, self.OnB9Button, id=wxID_FPRINCIPAL2B9)

        self.Idn = wx.StaticText(id=wxID_FPRINCIPAL2IDN, label=u'Idn',
              name='Idn', parent=self.panel1, pos=wx.Point(180, 60),
              size=wx.Size(20, 20), style=0)
        self.Idn.SetFont(wx.Font(6, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'Sans'))

        self.N = wx.StaticText(id=wxID_FPRINCIPAL2N, label=u'N', name='N',
              parent=self.panel1, pos=wx.Point(180, 60), size=wx.Size(20, 20),
              style=0)
        self.N.SetFont(wx.Font(6, wx.SWISS, wx.NORMAL, wx.BOLD, False,u'Sans'))

        self.B0 = wx.Button(id=wxID_FPRINCIPAL2B0, label=u'0', name=u'B0',
              parent=self.panel1, pos=wx.Point(180, 60), size=wx.Size(50, 50),
              style=0)
        self.B0.Bind(wx.EVT_BUTTON, self.OnB0Button, id=wxID_FPRINCIPAL2B0)

        self.entrar = wx.Button(id=wxID_FPRINCIPAL2ENTRAR, label=u'Entrar',
              name=u'entrar', parent=self.panel1, pos=wx.Point(180, 112),
              size=wx.Size(116, 50), style=0)
        self.entrar.Bind(wx.EVT_BUTTON, self.OnEntrarButton,
              id=wxID_FPRINCIPAL2ENTRAR)

        self.textCodigo = wx.TextCtrl(id=wxID_FPRINCIPAL2TEXTCODIGO,
              name=u'textCodigo', parent=self.panel1, pos=wx.Point(30, 24),
              size=wx.Size(225, 30), style=wx.TE_PASSWORD, value=u'')

        self.salir = wx.Button(id=wxID_FPRINCIPAL2SALIR, label=u'Salir',
              name=u'salir', parent=self.panel1, pos=wx.Point(180, 165),
              size=wx.Size(116, 50), style=0)
        self.salir.Bind(wx.EVT_BUTTON, self.OnSalirButton,
              id=wxID_FPRINCIPAL2SALIR)

        self.bclear = wx.Button(id=wxID_FPRINCIPAL2BCLEAR, label=u'Clear',
              name=u'bclear', parent=self.panel1, pos=wx.Point(230, 60),
              size=wx.Size(65, 50), style=0)
        self.bclear.Bind(wx.EVT_BUTTON, self.OnBclearButton,
              id=wxID_FPRINCIPAL2BCLEAR)

    def __init__(self, parent):
        self._init_ctrls(parent)

    def OnButton2Button(self, event):
        captura = self.textCodigo.GetValue ()
        captura = captura + '2'
        self.textCodigo.SetValue(captura)
        event.Skip()

    def OnB1Button(self, event):
        captura = self.textCodigo.GetValue ()
        captura = captura + '1'
        self.textCodigo.SetValue(captura) 
        event.Skip()

    def OnB4Button(self, event):
        captura = self.textCodigo.GetValue ()
        captura = captura + '4'
        self.textCodigo.SetValue(captura)
        event.Skip()

    def OnB7Button(self, event):
        captura = self.textCodigo.GetValue ()
        captura = captura + '7'
        self.textCodigo.SetValue(captura)
        event.Skip()

    def OnB2Button(self, event):
        captura = self.textCodigo.GetValue ()
        captura = captura + '2'
        self.textCodigo.SetValue(captura)
        event.Skip()

    def OnB3Button(self, event):
        captura = self.textCodigo.GetValue ()
        captura = captura + '3'
        self.textCodigo.SetValue(captura)
        event.Skip()

    def OnB8Button(self, event):
        captura = self.textCodigo.GetValue ()
        captura = captura + '8'
        self.textCodigo.SetValue(captura)
        event.Skip()

    def OnB9Button(self, event):
        captura = self.textCodigo.GetValue ()
        captura = captura + '9'
        self.textCodigo.SetValue(captura)
        event.Skip()

    def OnB0Button(self, event):
        captura = self.textCodigo.GetValue ()
        captura = captura + '0'
        self.textCodigo.SetValue(captura)
        event.Skip()

    def OnEntrarButton(self, event):
        Codigo1 = self.textCodigo.GetValue ()
        if Codigo1!= '':
		 	
		    conn_string = "host='localhost' port='5432' dbname='DB778' user='postgres' password='jorge'" 
		    conn = psycopg2.connect(conn_string)
		    cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
		    cursor.execute("SELECT * FROM catedratico WHERE codigo_catedratico ='%s'" % (Codigo1))
		    rows = cursor.fetchall()
		    for row in rows:
			    self.Idn.SetLabel (str(row["codigo_catedratico"]))
			    self.N.SetLabel (str(row["nombre"])) 
			    #print "%s %s %s %s" % (row["id_usuario"], row["nombre"], row["apellido"], row["codigo"])
			    		    
		    conn.close()
        else :
			
            dlg = wx.MessageDialog(self, 'Campo Vacio\t\n' 'Ingrese su Codigo de Usuario:\t\n','',wx.OK | wx.ICON_ERROR)
            dlg.ShowModal()
            dlg.Destroy()
        TempCodigo2 = self.Idn.GetLabel ()
        if (TempCodigo2!=0 or TempCodigo2!='') and TempCodigo2!='Idn':
            dlg = wx.MessageDialog(self, 'Bienvenido Usuario', 'Info', wx.OK | wx.ICON_INFORMATION)
            dlg.ShowModal()
            dlg.Destroy()
            #wx.MessageBox('Bienvenido', 'Info', wx.OK | wx.ICON_INFORMATION)
            #dlg.ShowModal()
            #dlg.Destroy()
            framep1=FAsistencia(None,TempCodigo2)
            framep1.Show(True)
            print TempCodigo2
            self.Close(True)
        else:
            dlg = wx.MessageDialog(self, 'Usuario incorrecto','error',wx.OK | wx.ICON_ERROR)
            dlg.ShowModal()
            dlg.Destroy()     
        

    def OnSalirButton(self, event):
        self.Close(True)
        event.Skip()

    def OnB5Button(self, event):
        captura = self.textCodigo.GetValue ()
        captura = captura + '5'
        self.textCodigo.SetValue(captura)
        event.Skip()

    def OnB6Button(self, event):
        captura = self.textCodigo.GetValue ()
        captura = captura + '6'
        self.textCodigo.SetValue(captura)
        event.Skip()

    def OnBclearButton(self, event):
        self.textCodigo.SetValue('')
        event.Skip()

